/* gtrans - Translation helped by Google
 * Copyright (C) 2011 Nagy Attila
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>
#include <glib/gi18n.h>
#include "settings.h"
#include "../config.h"

using namespace std;

Settings::Settings (languages_map& langs/*, const Glib::RefPtr<Gio::Settings>& settings*/) :
    languages (langs),
    //m_refSettings (settings),
    m_enabled_checkbox (_("Enabled")),
    m_listen_source_checkbox (_("Listen")),
    m_listen_target_checkbox (_("Listen")),
    m_source_label (_("Source language")),
    m_target_label (_("Target language")),
    m_api_key_label (_("Google Translate API key"))
{
    m_refSettings = Gio::Settings::create (CONF_HOME);
    m_refSettings->bind (CONF_ENABLED, &m_enabled_checkbox, "active");
    m_refSettings->bind (CONF_LISTEN_SOURCE, &m_listen_source_checkbox, "active");
    m_refSettings->bind (CONF_LISTEN_TARGET, &m_listen_target_checkbox, "active");
    m_refSettings->bind (CONF_GOOGLE_API_KEY, &m_api_key_entry, "text");

    set_title (string(PACKAGE_NAME " ") + _("Settings"));
    set_border_width (10);

    m_notebook.append_page (m_vbox_general, _("General"));
    m_notebook.append_page (m_vbox_advanced, _("Advanced"));

    get_vbox ()->pack_start (m_notebook);

    m_vbox_general.set_border_width (5);

    // enabled
    m_vbox_general.pack_start (m_enabled_checkbox);

    // lang box
    m_lang_box.set_spacing (10);
    m_vbox_general.pack_start (m_lang_box, false, false, 10);

    // source box
    m_source_box.set_spacing (10);
    m_lang_box.pack_start (m_source_box);

    m_source_box.pack_start (m_source_label);

    std::map<std::string, std::string, classcomp> ordered_langs;
    for (languages_iterator i = languages.begin (); i != languages.end (); ++i)
    {
        ordered_langs[(*i).second] = (*i).first;
    }

    // source lang
    m_refSourceLangModel = Gtk::ListStore::create (m_source_lang_columns);
    m_source_lang_combobox.set_model (m_refSourceLangModel);

    for (std::map<std::string, std::string, classcomp>::iterator i = ordered_langs.begin (); i != ordered_langs.end (); ++i)
    {
        Gtk::TreeModel::Row row = *(m_refSourceLangModel->append ());
        if (i == ordered_langs.begin ())
        {
            row[m_source_lang_columns.m_col_id] = AUTO_DETECT_SOURCE_LANG;
            string detect_language;
            detect_language += "--- ";
            detect_language += _("Detect language");
            detect_language += " ---";
            row[m_source_lang_columns.m_col_name] = detect_language;
            row = *(m_refSourceLangModel->append ());
        }
        row[m_source_lang_columns.m_col_id] = (*i).second;
        row[m_source_lang_columns.m_col_name] = (*i).first;
    }
    m_source_lang_combobox.pack_start (m_source_lang_columns.m_col_name);
    m_refSourceLangModel->foreach_iter (sigc::mem_fun (*this, &Settings::on_source_lang_item));
    m_source_lang_combobox.signal_changed ().connect (sigc::mem_fun (*this, &Settings::on_source_lang_changed));
    m_source_box.pack_start (m_source_lang_combobox);

    m_source_box.pack_start (m_listen_source_checkbox);

    // target box
    m_target_box.set_spacing (10);
    m_lang_box.pack_start (m_target_box);

    m_target_box.pack_start (m_target_label);

    // target lang
    m_refTargetLangModel = Gtk::ListStore::create (m_target_lang_columns);
    m_target_lang_combobox.set_model (m_refTargetLangModel);
    for (std::map<std::string, std::string, classcomp>::iterator i = ordered_langs.begin (); i != ordered_langs.end (); ++i)
    {
        Gtk::TreeModel::Row row = *(m_refTargetLangModel->append ());
        row[m_target_lang_columns.m_col_id] = (*i).second;
        row[m_target_lang_columns.m_col_name] = (*i).first;
    }
    m_target_lang_combobox.pack_start(m_target_lang_columns.m_col_name);
    m_refTargetLangModel->foreach_iter (sigc::mem_fun (*this, &Settings::on_target_lang_item));
    m_target_lang_combobox.signal_changed ().connect (sigc::mem_fun (*this, &Settings::on_target_lang_changed));
    m_target_box.pack_start (m_target_lang_combobox);

    m_target_box.pack_start (m_listen_target_checkbox);

    // Advanced

    m_vbox_advanced.set_border_width (5);

    m_api_key_label.set_alignment (0, 0);
    m_vbox_advanced.pack_start (m_api_key_label, false, false);
    m_vbox_advanced.set_spacing (10);
    m_vbox_advanced.pack_start (m_api_key_entry, false, false);

    // Dialog buttons

    add_button (Gtk::Stock::CLOSE, 0);

    show_all ();
}

Settings::~Settings ()
{
    /* WORKAROUND: Gio::Settings binding clears api-key if entry contains selection */
    m_api_key_entry.select_region (0, 0);
}

void Settings::on_source_lang_changed ()
{
    Gtk::TreeModel::iterator iter = m_source_lang_combobox.get_active ();
    if (iter)
    {
        Gtk::TreeModel::Row row = *iter;
        if (row)
        {
            Glib::ustring id = row[m_source_lang_columns.m_col_id];
            Glib::ustring name = row[m_source_lang_columns.m_col_name];
            m_refSettings->set_string (CONF_LANG_SOURCE, id);
        }
    }
}

void Settings::on_target_lang_changed ()
{
    Gtk::TreeModel::iterator iter = m_target_lang_combobox.get_active ();
    if (iter)
    {
        Gtk::TreeModel::Row row = *iter;
        if (row)
        {
            Glib::ustring id = row[m_target_lang_columns.m_col_id];
            Glib::ustring name = row[m_target_lang_columns.m_col_name];
            m_refSettings->set_string (CONF_LANG_TARGET, id);
        }
    }
}

bool Settings::on_source_lang_item (Gtk::TreeIter node)
{
    Gtk::TreeModel::Row row = *node;
    if (row)
    {
        Glib::ustring id = row[m_source_lang_columns.m_col_id];
        Glib::ustring name = row[m_source_lang_columns.m_col_name];
        Glib::ustring source_lang = m_refSettings->get_string (CONF_LANG_SOURCE);
        if (source_lang == id)
        {
            m_source_lang_combobox.set_active (node);
            return true;
        }
    }
    return false;
}

bool Settings::on_target_lang_item (Gtk::TreeIter node)
{
    Gtk::TreeModel::Row row = *node;
    if (row)
    {
        Glib::ustring id = row[m_target_lang_columns.m_col_id];
        Glib::ustring name = row[m_target_lang_columns.m_col_name];
        Glib::ustring target_lang = m_refSettings->get_string (CONF_LANG_TARGET);
        if (target_lang == id)
        {
            m_target_lang_combobox.set_active (node);
            return true;
        }
    }
    return false;
}

